package com.wzdigit.wms.wms.controller.stockIn;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.request.asn.initial.SubmitInitialInInputRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.asn.initial.ScanInitialSnResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.order.InitialStockInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@RestController
@Api(tags = "初始化入库")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class InitialStockInController {

    private final static String REQUEST_MODEL = "/InitialStockIn";

    @Autowired
    private InitialStockInService initialStockInService;

	/**
	 * 扫描SN
	 * @param sn
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "扫描SN", notes = "扫描SN")
	@ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
	@GetMapping(value = REQUEST_MODEL + "/scanSn")
	public Result<ScanInitialSnResponse> scanSn(String sn, HttpServletRequest servletRequest,
			HttpServletResponse servletResponse) {
		try {
			return initialStockInService.scanSn(sn, servletRequest);
		} catch (RestStatusException ex) {
			servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			throw ex;
		}
	}

    /**
     * 提交
     * @param input
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception 
     */
    @ApiOperation(value = "提交数据", notes = "提交数据")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/submit", method = RequestMethod.POST)
    public Result<String> submit(@RequestBody @Valid SubmitInitialInInputRequest input,
    		HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        try {
        	return initialStockInService.submit(input, servletRequest);
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }
}